package com.zynn.service.module.es.controller;

import com.zynn.common.core.base.BaseController;
import com.zynn.common.pojo.result.Result;
import com.zynn.common.pojo.result.ResultUtil;
import com.zynn.service.module.es.service.SyncEventService;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@Validated
@RequestMapping("sync/event")
public class SyncEventController extends BaseController {

    @Autowired
    private SyncEventService syncEventService;


    /**
     * 创建动态照片
     *
     * @param groupId     相册id
     * @param location    发布的详细位置
     * @param title       文字标题
     * @param content     文字内容
     * @param publishTime 发布时间
     * @return 创建动态照片是否成功
     */
    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST)
    public Result saveEvent(@RequestParam @Min(0) Long eventId,
                            @RequestParam @NotNull Integer eventType,
                            @RequestParam @Min(0) Long userId,
                            @RequestParam @Min(0) Long groupId,
                            String accessGroupIds,
                            @RequestParam @NotNull Integer groupTypeValue,
                            @RequestParam @NotNull String publishTime,
                            String location,
                            String title,
                            String content,
                            Double latitude,
                            Double longitude,
                            Integer commentCount,
                            Integer likeCount,
                            Integer resourceCount) {
        log.info("sync/event/createOrUpdate传入的eventId:{}，publishTime:{}", eventId, publishTime);
        syncEventService.syncOneEvent(eventId, eventType, userId, groupId, accessGroupIds, groupTypeValue, location, title, content, publishTime, latitude, longitude, commentCount, likeCount, resourceCount);
        return ResultUtil.success();
    }

    /**
     * 同步动态，不传publishTime
     * @param eventId
     * @param eventType
     * @param userId
     * @param groupId
     * @param accessGroupIds
     * @param groupTypeValue
     * @param location
     * @param title
     * @param content
     * @param latitude
     * @param longitude
     * @param commentCount
     * @param likeCount
     * @param resourceCount
     * @return
     */
    @RequestMapping(value = "/createOrUpdateWithOutPublishTime", method = RequestMethod.POST)
    public Result createOrUpdateWithOutPublishTime(@RequestParam @Min(0) Long eventId,
                            @RequestParam @NotNull Integer eventType,
                            @RequestParam @Min(0) Long userId,
                            @RequestParam @Min(0) Long groupId,
                            String accessGroupIds,
                            @RequestParam @NotNull Integer groupTypeValue,
                            String location,
                            String title,
                            String content,
                            Double latitude,
                            Double longitude,
                            Integer commentCount,
                            Integer likeCount,
                            Integer resourceCount) {
        syncEventService.syncOneEvent(eventId, eventType, userId, groupId, accessGroupIds, groupTypeValue, location, title, content, null, latitude, longitude, commentCount, likeCount, resourceCount);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result removeEvent(@RequestParam @Min(0) Long eventId) {
        syncEventService.remove(eventId);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateEvent(@RequestParam @Min(0) Long eventId, Integer commentCount, Integer likeCount, Integer resourceCount) {
        syncEventService.update(eventId, commentCount, likeCount, resourceCount);
        return ResultUtil.success();
    }
}
